<?php
/**
 * Created by PhpStorm.
 * User: ASUS
 * Date: 2017/6/23
 * Time: 14:37
 */

namespace Clients;

// 同步客户端
class ClientSync
{
    private $client;

    public function __construct() {
        // 默认可以不填，就是SWOOLE_SOCK_SYNC 同步客户端，不可以使用回调函数的写法
        $this->client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
    }

    public function connect() {
        if( !$this->client->connect("127.0.0.1", 9501 , 1) ) {
            exit("connect failed. Error: {$this->client->errCode}\n");
        }

        // php cli 常量
        fwrite(STDOUT, "请输入消息:\n");
        $msg = trim(fgets(STDIN));

        $this->client->send( $msg );

        $message = $this->client->recv();
        echo "Get Message From Server:{$message}\n";
        sleep(10);
    }
}

$client = new ClientSync();
$client->connect();